package leetcode_1001_1100;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class LeeCode_1090 {
    public static void main(String[] args) {

    }
    private static int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
        Map<Integer, Integer> map = new HashMap<>();
        int n = values.length;
        Integer[] is = new Integer[n];
        for (int i = 0; i < n; i++) {
            is[i] = i;
        }
        Arrays.sort(is, (o1, o2) -> values[o2] - values[o1]);
        int ans = 0, c = 0;
        for (int i = 0; i < n; i++) {
            if (c == numWanted){
                break;
            }
            int index = is[i];
            if (map.getOrDefault(labels[index], 0) < useLimit){
                map.put(labels[index], map.getOrDefault(labels[index], 0) + 1);
                ans += values[index];
                c++;
            }
        }
        return ans;
    }
}
